﻿using Com.Lancetinc.Evs.Communications;
using Com.Lancetinc.Evs.Data;
using Com.Lancetinc.Evs.Data.Domain;
using Com.Lancetinc.Evs.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace Com.Lancetinc.Evs.Services
{
    class RunningExperControlInfo
    {
        /// <summary>
        /// 流水号
        /// </summary>
        public string SeqNo { get; set; }

        /// <summary>
        /// 录像开始时间
        /// </summary>
        public string StartTime { get; set; }

        /// <summary>
        /// 录像结束时间
        /// </summary>
        public string EndTime { get; set; }
    }

    public static class ExperRunController
    {
        public static async Task<ExperRunParams> PrepareRun(InstrumentManager instManager, ExperInfo exper, int retryTimes)
        {
            var r = await VideoStartRecordHelper.PrepareStartRecord(instManager, exper, retryTimes);
            if (r == null)
            {
                return null;
            }
            SaveControlInfo(exper.Exper.Exper);
            return r;
        }

        public static void ReportState(Exper exper)
        {
            if (exper.EndTime != null)
            {
                var path = GetRunningExperControlFile();
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
            }
            else
            {
                SaveControlInfo(exper);
            }
        }

        private static string GetRunningExperControlFile()
        {
            var path = AppDomain.CurrentDomain.BaseDirectory;
            path = Path.Combine(path, "exper_run_control.ini");
            return path;
        }

        private static void SaveControlInfo(Exper exper)
        {
            var path = GetRunningExperControlFile();
            var lines = new List<string>
            {
                "SeqNo=" + exper.SeqNo,
                "StartTime=" + exper.StartTime,
                "EndTime=" + (exper.EndTime ?? DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
            };
            try
            {
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                File.WriteAllLines(path, lines);
            }
            catch (Exception) { }
        }

        private static RunningExperControlInfo LoadControlInfo()
        {
            var path = GetRunningExperControlFile();
            if (!File.Exists(path))
            {
                return null;
            }
            var lines = File.ReadAllLines(path);
            var info = new RunningExperControlInfo();
            foreach (var line in lines)
            {
                var ss = line.Split('=');
                if (ss.Length == 2)
                {
                    if (ss[0] == "SeqNo")
                    {
                        info.SeqNo = ss[1];
                    }
                    else if (ss[0] == "StartTime")
                    {
                        info.StartTime = ss[1];
                    }
                    else if (ss[0] == "EndTime")
                    {
                        info.EndTime = ss[1];
                    }
                }
            }
            return info;
        }

        /// <summary>
        /// 程序启动后，调用此方法修改运行过程中的异常
        /// </summary>
        public static void RepairExperEndTimeWhenAppStarted()
        {
            var info = LoadControlInfo();
            if (info == null) return;
            File.Delete(GetRunningExperControlFile());

            Exper exper = AppRepositories.Exper.FindBySeqNo(info.SeqNo);
            if (exper != null && string.IsNullOrEmpty(exper.EndTime))
            {
                exper.EndTime = info.EndTime;
                AppRepositories.Exper.Save(exper);
            }
        }
    }
}
